package com.ruoyi.service.impl;

import com.github.pagehelper.PageInfo;
import com.hundun.vision.api.common.PlainResult;
import com.hundun.vision.api.dto.ProfitDTO;
import com.hundun.vision.api.dto.TradeDTO;
import com.hundun.vision.api.query.ProfitQuery;
import com.hundun.vision.api.service.RPCProfitService;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.ResultUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.service.ProfitService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author ：RuoChen
 * @date ：11:22 2021/6/25
 * @description：收益服务类实现类
 */
@Slf4j
@Component
public class ProfitServiceImpl implements ProfitService {

    @DubboReference(timeout = 60000, retries = 0)
    private RPCProfitService profitService;

    @Override
    public PageInfo<ProfitDTO> search(ProfitQuery query) {
        log.info("search feature,query:{}", query);
        LoginUser loginUser = SecurityUtils.getLoginUser();
        SysUser user = loginUser.getUser();
        query.setUserId(user.getUserId());
        PlainResult<PageInfo<ProfitDTO>> result = profitService.search(query);
        if (result.isSuccess()) {
            return result.getData();
        } else {
            ResultUtils.error(result.getMessage());
        }
        return null;
    }

    @Override
    public List<TradeDTO> getTrades(Long robotId) {
        log.info("get trades,robotId:{}", robotId);
        LoginUser loginUser = SecurityUtils.getLoginUser();
        SysUser user = loginUser.getUser();
        PlainResult<List<TradeDTO>> result = profitService.getTrades(user.getUserId(), robotId);
        if (result.isSuccess()) {
            return result.getData();
        } else {
            ResultUtils.error(result.getMessage());
        }
        return null;
    }
}
